package com.uubx.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.uubx.exception.CustomerException;

import com.uubx.result.MessageCode;
import com.uubx.result.Result;
import com.uubx.result.ResultUtils;
import com.uubx.service.OderService;
import com.uubx.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/*
hxd
 */
@Controller
@RequestMapping("/oder")
@ResponseBody
@Api(description = "订单的相关接口")
public class OderController {

    @Autowired
    private OderService oderService;

    //1分页查询
    @RequestMapping(value = "/allList",method = RequestMethod.POST)
    @ApiOperation(value = "条件分页查询",notes = "条件分页查询",httpMethod = "POST")
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功! data代表返回的数据"),
            @ApiResponse(code=500,message="后台逻辑错误!"),
            @ApiResponse(code=404,message="找不到页面!"),
            @ApiResponse(code=405,message="请求方式不正确，比如后台接收是post，但前端请求的是get!"),
            @ApiResponse(code=406,message="页面地址和接口名冲突!"),
            @ApiResponse(code=415,message="请求格式不正确，前端可能没有把请求头修改为json，也可能前端请求的json对象没有转换为字符串!")
    })
    public Result list(@RequestBody OderVo oderVo) throws CustomerException {

        PageHelper.offsetPage(oderVo.getStartIndex(),oderVo.getPageSize());
        List<Map> list = oderService.findOderWhere(oderVo);
        PageInfo<Map> pageInfo=new PageInfo<>(list);

       if(pageInfo.getList().isEmpty()){
           throw  new CustomerException(MessageCode.ODER_EMPTY);
       }

        return ResultUtils.buildSuccess(pageInfo);

    }



    //2通过id跳转
    @RequestMapping(value = "/findId",method = RequestMethod.GET)
    @ApiOperation(value = "根据id查询",notes = "根据id查询",httpMethod = "get")
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功! data代表返回的数据"),
            @ApiResponse(code=500,message="后台逻辑错误!"),
            @ApiResponse(code=404,message="找不到页面!"),
            @ApiResponse(code=405,message="请求方式不正确，比如后台接收是post，但前端请求的是get!"),
            @ApiResponse(code=406,message="页面地址和接口名冲突!"),
            @ApiResponse(code=415,message="请求格式不正确，前端可能没有把请求头修改为json，也可能前端请求的json对象没有转换为字符串!")
    })
    public Result findId(Long id) throws CustomerException {

        if(id==null){
            throw  new CustomerException(MessageCode.ODER_ID);
        }

    List<Map> oderById=oderService.findOderById(id);
        return  ResultUtils.buildSuccess(oderById);

    }


}
